package client_gui;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

/**
 * Klasa koja predstavlja RegisterPanel, odakle se aktivira dijalog za registraciju.
 * 
 * @author UrosKrkic & Radivoje Popovic
 */
public class RegisterPanel extends JPanel {
	
	/**
	 * Vlasnik - glavni prozor aplikacije.
	 */
	private ClientWindow owner;
	
	
	/**
	 * Layout manager - GridBagLayout
	 */
	private GridBagLayout gridBag = null;
	
	
	// ogranicenja u GridBagLayout-u
	private GridBagConstraints registerLabelConstraint = null;
	private GridBagConstraints registerButtonConstraint = null;
	private GridBagConstraints signatureConstraint = null;
	
	
	/**
	 * Border.
	 */
	private TitledBorder titledBorder;
	
	
	// komponente ovog panela
	private JLabel registerLabel = null;
	private JButton registerButton = null;
	private JLabel signature = null;
	
	
	/**
	 * Konstruktor.
	 * @param owner - glavni prozor aplikacije
	 * @author UrosKrkic & Radivoje Popovic
	 */
	public RegisterPanel(ClientWindow owner) {
		this.owner = owner;
		
		this.gridBag = new GridBagLayout();
		
		registerLabelConstraint = new GridBagConstraints();
		registerButtonConstraint = new GridBagConstraints();
		signatureConstraint = new GridBagConstraints();
		
		titledBorder = BorderFactory.createTitledBorder(BorderFactory.
				createEtchedBorder(EtchedBorder.RAISED),
				"Register", javax.swing.border.TitledBorder.CENTER,
				javax.swing.border.TitledBorder.DEFAULT_POSITION,
				null, null);
		
		initializeRegisterPanel();
	}
	
	
	
	/**
	 * Metod vrsi inicijalizaciju ovog panela.
	 * @author UrosKrkic & Radivoje Popovic
	 */
	private void initializeRegisterPanel() {
		this.setLayout(gridBag);
		this.setBorder(titledBorder);

		
		// inicijalizacija ogranicenja LayoutManager-a
		initializeConstraints();
		
		// dodavanje komponenti u panel
		this.add(getRegisterLabel());
		this.add(getRegisterButton());
		this.add(getSignature());
	}
	
	
	/**
	 * Metod vrsi inicijalizaciju ogranicenja LayoutManager-a.
	 * @author UrosKrkic & Radivoje Popovic
	 */
	private void initializeConstraints() {
		registerLabelConstraint.gridx = 0;
		registerLabelConstraint.gridy = 0;
		registerLabelConstraint.weightx = 1;
		registerLabelConstraint.weighty = 1;
		registerLabelConstraint.anchor = GridBagConstraints.NORTHWEST;
		registerLabelConstraint.insets = new Insets(10,10,0,10);
		
		registerButtonConstraint.gridx = 0;
		registerButtonConstraint.gridy = 1;
		registerButtonConstraint.weightx = 1;
		registerButtonConstraint.weighty = 1;
		registerButtonConstraint.anchor = GridBagConstraints.NORTHWEST;
		registerButtonConstraint.insets = new Insets(0,10,40,10);
		
		signatureConstraint.gridx = 0;
		signatureConstraint.gridy = 2;
		signatureConstraint.weightx = 1;
		signatureConstraint.weighty = 1;
		signatureConstraint.anchor = GridBagConstraints.SOUTHEAST;
		signatureConstraint.insets = new Insets(0,0,5,10);
	}
	
	
	
	
	// Metode koje inicijalizuju komponente ovog panela.
	
	private JLabel getRegisterLabel() {
		if (registerLabel == null) {
			registerLabel = new JLabel("Register a new account?");
			gridBag.setConstraints(registerLabel, registerLabelConstraint);
		}
		
		return registerLabel;
	}
	
	
	private JButton getRegisterButton() {
		if (registerButton == null) {
			registerButton = new JButton("Register");
			registerButton.setToolTipText("Registracija korisnika");
			gridBag.setConstraints(registerButton, registerButtonConstraint);
			registerButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					RegisterDialog registerDialog = new RegisterDialog(owner);
					registerDialog.setVisible(true);
				}
			});
		}
		
		return registerButton;
	}
	
	
	private JLabel getSignature() {
		if (signature == null) {
			signature = new JLabel("RLM Team - Copyright 2008.");
			signature.setFont(new Font("Arial", Font.ITALIC, 12));
			gridBag.setConstraints(signature, signatureConstraint);
		}
		
		return signature;
	}

}
